package me.dz.dreamcleaner.listener;

import me.dz.dreamcleaner.utils.LogUtils;
import me.dz.dreamcleaner.utils.TrashUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Objects;

import static me.dz.dreamcleaner.utils.TrashUtils.*;
import static me.dz.dreamcleaner.DreamCleaner.*;
import static me.dz.dreamcleaner.utils.MsgUtils.*;

public class TrashEvent implements Listener {
    @EventHandler
    public void onClick(InventoryClickEvent e){
        if (e.getSlot() == -999)return;
        if (e.getView().getTitle() == null)return;
        if (e.getView().getTitle().contains(trashTitle)){
            e.setCancelled(true);
            ItemStack itemStack = e.getCurrentItem();
            if (itemStack != null && itemStack.getType() != Material.AIR){
                itemStack = itemStack.clone();
                Player p = (Player) e.getWhoClicked();
                if (lock){
                    chat(p,lang("trashLock"));
                    return;
                }
                if (e.getClickedInventory() == p.getInventory()){
                    if (!trashItem){
                        if (e.getView().getTopInventory().firstEmpty() == -1){
                            chat(p,lang("trashFill"));
                        }else {
                            if (TrashUtils.canJoin(itemStack)){
                                e.setCurrentItem(null);
                                p.getInventory().setItem(e.getSlot(),null);
                                e.getView().getTopInventory().addItem(itemStack.clone());
                                if (logEnable && logTrash){
                                    LogUtils.saveTrash(p,itemStack.clone(),true);
                                }
                            }else {
                                chat(p,lang("trashItem"));
                            }
                        }
                    }else {
                        chat(p,lang("trashItem"));
                    }
                }else {
                    int n = Integer.parseInt(e.getView().getTitle().replace(trashTitle,""));
                    if (e.getSlot() == 45){
                        if (n == 1){
                            chat(p,lang("trashUp"));
                        }else {
                            open(p,n-2);
                        }
                    }else if (e.getSlot() == 53){
                        if (n == trashSize){
                            chat(p,lang("trashDown"));
                        }else {
                            open(p,n);
                        }
                    }else {
                        if (p.getInventory().firstEmpty() == -1){
                            chat(p,lang("trashPlayer"));
                        }else {
                            e.setCurrentItem(null);
                            Objects.requireNonNull(e.getClickedInventory()).setItem(e.getSlot(),null);
                            p.getInventory().addItem(itemStack.clone());
                            if (logEnable && logTrash){
                                LogUtils.saveTrash(p,itemStack.clone(),false);
                            }
                        }
                    }
                }
            }
        }
    }

    @EventHandler
    public void onClose(InventoryCloseEvent e){
        if (e.getPlayer() instanceof Player){
            Player p = (Player) e.getPlayer();
            if (e.getView().getTitle().contains(trashTitle)){
                open.remove(p);
            }
        }
    }
}
